﻿(function ($) {

    $.confirm = function (params) {

        if ($('#ModalOverlayMb').length) {
            // A confirm is already shown on the page:
            return false;
        };

        var buttonHTML = '';
        $.each(params.buttons, function (name, obj) {

            buttonHTML += '<a href="#" ><em></em>' + name + '</a>';

            if (!obj.action) {
                obj.action = function () { };
            }
        });

        var markup = [
			    '<div id="mbBox" class="messagePanel mb', params.classN, '" >',
                    '<table>',
                        '<tr>',
                            '<td rowspan="2" class="pict"></td>',
                            '<td class="text">', params.message, '</td>',
                        '</tr>',
                        '<tr>',
                            ' <td class="btn">', buttonHTML, '</td>',
                        '</tr>',
                    '</table>',
                '</div>',
                '<div id="ModalOverlayMb">',
			'</div>'
		].join('');

        $(markup).hide().appendTo('body').fadeIn(1);

        //var buttons = $('#confirmBox a'),
        var buttons = $('#mbBox a'),
			i = 0;

        $.each(params.buttons, function (name, obj) {
            buttons.eq(i++).click(function () {

                // Calling the action attribute when a
                // click occurs, and hiding the confirm.

                obj.action();
                $.confirm.hide();
                return false;
            });
        });
    }

    $.confirm.hide = function () {
        $('#ModalOverlayMb, #mbBox').fadeOut(1, function () {
            $(this).remove();
        });
    }

})(jQuery);